package cn.lopr.im.android.mvp.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.IPersonalHomepageContact;
import cn.lopr.im.android.utils.Apis;
import cn.lopr.im.android.utils.HttpUtils;

public class PersonalHomepageModel implements IPersonalHomepageContact.IPersonalHomepageModel {


    @Override
    public void getUserInfo(String token, OnCallBackListener<CallbackResult<User>, String> listener) {

        HttpUtils.doGetForResult(Apis.WebApis.GET_USER_INFO.getValue(), "", token, data -> {

            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                listener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<User>>() {
            }.getType();
            CallbackResult<User> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                listener.success(callbackResult);

            } else {
                listener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getUserInfoById(String token, int userId, OnCallBackListener<CallbackResult<User>, String> listener) {
        String param = String.format("?userId=%s", userId);
        HttpUtils.doGetForResult(Apis.WebApis.GET_USER_INFO_BY_ID.getValue(), param, token, data -> {

            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                listener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<User>>() {
            }.getType();
            CallbackResult<User> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                listener.success(callbackResult);

            } else {
                listener.failure(callbackResult.getMsg());

            }
        });
    }


}
